/**
 * 
 */
package com.prueba;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author jaime
 *
 */
public class Principal {

	public static boolean executeCommand ( String command, boolean flagbackground) 
	{	
		// Definimos la cadena del interprete de comandos del sistema 
		String commandShell=null; 
	
		// Recuperamos el sistema operativo 
		String osName = System.getProperty ( "os.name" ); 
	
		// Cargamos la cadena del interprete de comandos según el sistema operativo y el comando a ejecutar 
		if ( osName.equals ("Windows NT") ) 
			commandShell = "cmd.exe /C " + command; 
		else if ( osName.equals ("Windows 95") || osName.equals ("Windows 98") ) 
			commandShell = "start " + command; 
		else{ 
			// En UNIX y LUNUX podemos lanzar el proceso en background sufijandolo con & 
			if (flagbackground) 
				commandShell = "" + command +" &" ; 
			else 
				commandShell = "" + command ; 
		} 
	
		// Lanzamos el proceso	
		try{ 
			Process proc = Runtime.getRuntime ().exec (commandShell); 
			BufferedReader brStdOut = new BufferedReader(new InputStreamReader(proc.getInputStream())); 
			BufferedReader brStdErr = new BufferedReader(new InputStreamReader(proc.getErrorStream())); 
			String str=null; 
			while ((str = brStdOut.readLine())!=null){ 
				System.out.println (str); 
			} 
			brStdOut.close(); 
			brStdErr.close(); 
		} 
		catch (IOException eproc){ 
			System.out.println ("Error to execute the command : "+eproc); 
			return false; 
		} 
			return true; 
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(args != null){
			executeCommand(args[0], false);
		}

	}

}
